package com.yanyeori.rabbitmqprovider.config;

import com.yanyeori.rabbitmqprovider.constant.RabbitmqConfig;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author chenkuan
 * @version v1.0
 * @desc Topic Exchange(主题交换机)：按主题消费数据，可以模糊匹配主题
 * @date 20/7/30 030 15:43
 */
@Configuration
public class TopicRabbitConfig {
	/**
	 * 队列1 -- 主题1
	 */
	@Bean
	public Queue topicQueue1() {
		return new Queue(RabbitmqConfig.TOPIC_MAN);
	}

	/**
	 * 队列2 -- 主题2
	 */
	@Bean
	public Queue topicQueue2() {
		return new Queue(RabbitmqConfig.TOPIC_WOMAN);
	}

	/**
	 * 交换机
	 */
	@Bean
	public TopicExchange topicExchange() {
		return new TopicExchange(RabbitmqConfig.TOPIC_EXCHANGE);
	}

	/**
	 * 将topicQueue1和topicExchange绑定,而且绑定的键值为topic.man
	 * 这样只要是消息携带的路由键是topic.man,才会分发到该队列
	 */
	@Bean
	public Binding topicBinding1(@Qualifier("topicQueue1") Queue queue, @Qualifier("topicExchange") TopicExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with(RabbitmqConfig.TOPIC_MAN);
	}

	/**
	 * 将topicQueue2和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
	 * 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
	 */
	@Bean
	public Binding topicBinding2(@Qualifier("topicQueue2") Queue queue, @Qualifier("topicExchange") TopicExchange exchange) {
		return BindingBuilder.bind(queue).to(exchange).with("topic.#");
	}
}
